﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web;
using System.IO;
using System.Text;
using Common;

using CSharpConnectMySQL;
using System.Data;

/*
 * 打标机
 */

namespace DingApi.Controllers
{
    [RoutePrefix("api/Print_MatNo")]
    public class Print_MatNoController : ApiController
    {

        string ps_name = "Print_MatNo";

        int i = get_order.get_order_num();
         

        [HttpPost]
        [Route("PostGetPrint_MatNo")]
        public System.Data.DataSet PostGetPrint_MatNo([FromBody]  JObject obj)
        {
            ClassMyFile.WriteLog("get mat_no  begin--------------------------------  ", ps_name);


          



            DateTime dt0 = DateTime.Now;
            DateTime dt1 = DateTime.Now;
            TimeSpan timeDifference;
            int subTime = 0;


            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables[0].TableName = "SYS";
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");



            //逻辑校验；
            ClassMyFile.WriteLog("本日调用次数[" + i.ToString() + "],上次调用时间=[" + get_order.get_dt_last().ToString("yyyy-MM-dd HH:mm:ss") + "]", ps_name);
             

            timeDifference = DateTime.Now - get_order.get_dt_last();
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;
            
            if (subTime <= 1500)
            {
                ClassMyFile.WriteLog("end---错误：请求间隔不得少于1.5秒", ps_name);

                outBlock.Tables["SYS"].Rows.Add(-1, "请求间隔不得少于1.5秒！");
                return outBlock;
            }






            string sql = "";
            try
            {
                 sql = "   SELECT  t.serial_number  prod_seqno   "
                                + "      , sg_std, sg_sign, mat_spec_desc, erp_heat_number pono_no   "
                                + "      , t.batch_number       lot_no   "
                                + "      , t.part_num           button_mum   "
                                + "       , FROM_UNIXTIME(t.start_flow_time / 1000, '%Y%m%d%H%i%s')  out_time   "
                                + "      , t.teams_groups shift_no   "
                                + "      , t.product_weight / 1000  mat_wt   "
                                + "      , t.hanger_number hook_id   "
                                + "      , t.style_code   " 
                                + "      ,t.heat_number backlog_code  "

                                + " FROM    t_material t  "
                                + "     WHERE t.flow_id  in ('chengZhong1','chengZhong2')   "
                                + "     ORDER BY     t.start_flow_time DESC    LIMIT 1  "
                                ;

                DataSet dataSet = MySqlHelper2.GetDataSet(sql);
                if (dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count == 0)
                {
                    ClassMyFile.WriteLog("end---无数据sql\n："+ sql + "", ps_name);
                    outBlock.Tables["SYS"].Rows.Add(-1, "无数据");
                    return outBlock;
                }
                else
                {

                    //第6位是流号，可能有7位，若有7位，第7位一般表示异常坯
                    if (dataSet.Tables[0].Rows[0]["BACKLOG_CODE"].ToString().Length >= 6)
                    {
                        dataSet.Tables[0].Rows[0]["BACKLOG_CODE"] = dataSet.Tables[0].Rows[0]["BACKLOG_CODE"].ToString().Substring(5, 1);
                    }
                    //若不对，前台画面可以修改，但不提示；详情中修改喷号，则同步修改！
                    else
                    {
                        dataSet.Tables[0].Rows[0]["BACKLOG_CODE"] = " ";
                    }
                    




                    if (dataSet.Tables[0].Rows[0]["SHIFT_NO"].ToString()=="A")
                    {
                        dataSet.Tables[0].Rows[0]["SHIFT_NO"] = "甲";
                    }
                    else if (dataSet.Tables[0].Rows[0]["SHIFT_NO"].ToString() == "B")
                    {
                        dataSet.Tables[0].Rows[0]["SHIFT_NO"] = "乙";
                    }
                    else if (dataSet.Tables[0].Rows[0]["SHIFT_NO"].ToString() == "C")
                    {
                        dataSet.Tables[0].Rows[0]["SHIFT_NO"] = "丙";
                    }
                    else if (dataSet.Tables[0].Rows[0]["SHIFT_NO"].ToString() == "D")
                    {
                        dataSet.Tables[0].Rows[0]["SHIFT_NO"] = "统计";
                    } 


                    outBlock.Tables["SYS"].Rows.Add(1, "成功");
                    outBlock.Tables.Add(dataSet.Tables[0].Copy());
                }
            }
            catch (Exception ex)
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "异常错误["+ex.ToString()+"]");

                ClassMyFile.WriteLog(string.Format("end----- -------异常错误{0}--\n{1}   ", ex.ToString(), sql), ps_name);

            }  

            timeDifference = DateTime.Now - dt0;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;
             

            ClassMyFile.WriteLog(string.Format("end--------------------------------总耗时{0}ms  ", subTime), ps_name); 
            get_order.set_dt_last(); 
            return outBlock;

        }


 

       










    }





}